//
//  ZCSettingTableViewController.m
//  BaiSi
//
//  Created by 张灿 on 16/4/25.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCSettingTableViewController.h"
#import <SDImageCache.h>
#import "ZCFileManager.h"

@interface ZCSettingTableViewController ()

@end

@implementation ZCSettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
}


#pragma mark - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = [self sizeString];
    
    return cell;
}

#pragma mark - 拼接缓存大小
- (NSString *)sizeString
{
    //获取cache文件夹路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    
    //获取缓存大小
    NSInteger size = [ZCFileManager getSizeOfDirectoryPath:cachePath];
    
    NSString *str = @"清除缓存";
    //判断尺寸大小,拼接不同的显示方式
    if (size > 1000 * 1000) {
        CGFloat sizeMB = size / 1000.0 / 1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fMB)", str, sizeMB];
    } else if (size > 1000) {
        CGFloat sizeKB = size / 1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fKB)", str, sizeKB];
    } else if (size > 0) {
        str = [NSString stringWithFormat:@"%@(%ldB)", str, size];
    }
    
    return str;
}


#pragma mark - 清除缓存
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取cache路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    // 清空缓存
    [ZCFileManager removeDirectoryPath:cachePath];
    
    //刷新tableView
    [self.tableView reloadData];
}

@end
